/*
 * [程序名称] Weather auto sender
 * [参考资料] http://code.google.com/p/auto-weather-sender/ 
 * [开源协议] Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.joe.weather;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.joe.weather.service.SmsObserveService;
import com.joe.weather.service.WeatherService;
import com.joe.weather.tool.PreferenceUtil;
import com.joe.weather.tool.WeatherUtil;

public class MainActivity extends Activity
{
	private static final String TAG = MainActivity.class.getSimpleName();
	
	private static final int SYSTEM_SETTING = Menu.FIRST;
	private static final int SYSTEM_EXIT = Menu.FIRST + 1;
	
	private SharedPreferences sp = null;
	private String currentCityCode = "";

	private LayoutInflater mInflater;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.logo);
		
		mInflater = getLayoutInflater();
		
		LogoTask task = new LogoTask(this);
		task.execute("");
	}

	private class LogoTask extends AsyncTask<Object, Object, View>
	{
		public LogoTask(Context context)
		{
		}

		protected View doInBackground(Object... params)
		{
			try
			{
				initSystemData();
				
				startWeatherService();
				startSmsObsveService();
				Log.e(TAG, "start 2 services.........");
				
				Thread.sleep(3000);
			}
			catch (InterruptedException e)
			{
				e.printStackTrace();
			}
			
			return mInflater.inflate(R.layout.main, null);
		}

		protected void onPostExecute(View view)
		{
			setContentView(view);
			
			currentCityCode = sp.getString("cityName", "101280101");
			
			PreferenceUtil preUtil = new PreferenceUtil(MainActivity.this);
			String callName = preUtil.getCurrentCallName();
			String cityName = preUtil.getCurrentCityName(currentCityCode);
			
			String str = WeatherUtil.getSMSMessage(callName, currentCityCode, cityName);
			TextView textView = (TextView) findViewById(R.id.textView);
			textView.setText(str);
		}
	}

	// 屏蔽按键
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			return true;
		}
		else if (keyCode == KeyEvent.KEYCODE_HOME)
		{
			return true;
		}
		return false;
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(Menu.NONE, SYSTEM_SETTING, Menu.NONE, "Setting");
		menu.add(Menu.NONE, SYSTEM_EXIT, Menu.NONE, "Exit");
		
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		super.onOptionsItemSelected(item);
		switch (item.getItemId())
		{
			case SYSTEM_SETTING:
				jumpToSetting();
				break;
			default:
				finish();
				exit();
				break;
		}

		return true;
	}

	private void exit()
	{
		ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
		am.killBackgroundProcesses("com.joe.weather");
	}
	
	private void jumpToSetting()
	{
		Intent intent = new Intent(MainActivity.this, SettingActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}

	public void sendWeatherButtonClicked(View view)
	{
		Button button = (Button) view;
		if ("现在发送".equals(button.getText()))
		{
			Log.e(TAG, "send button clicked......");
			button.setText("取消发送");
			startWeatherService();
		}
		else
		{
			Log.e(TAG, "send button clicked on cancel......");
			cancelSendWeather();
		}
		button.setText("现在发送");
	}

	private void startWeatherService()
	{
		Intent intent = new Intent(MainActivity.this, WeatherService.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startService(intent);
	}

	private void cancelSendWeather()
	{
		Intent intent = new Intent(MainActivity.this, WeatherService.class);
		stopService(intent);
	}

	private void initSystemData()
	{
		sp = PreferenceManager.getDefaultSharedPreferences(this);
		final String cityCode = sp.getString("cityName", "UnKnown");
		if("UnKnown".equals(cityCode))
		{
			Editor editor = sp.edit();
			editor.putBoolean("systemSwitch", true);
			editor.putBoolean("smsFuncSwitch", true);
			
			// set default send time to 8:00 PM
			editor.putString("sendTime", "20");
			// set the default city GuangZhou
			editor.putString("cityName", "101280101");
			// set default call name
			editor.putString("callName", "1");
			
			editor.commit();
		}
	}
	
	private void startSmsObsveService()
	{
		Intent intent = new Intent(MainActivity.this, SmsObserveService.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startService(intent);
	}
}
